<%@page import="com.sun.image.codec.jpeg.JPEGCodec"%>
<%@page import="com.sun.image.codec.jpeg.JPEGImageEncoder"%>
<%@page import="java.awt.Font"%>
<%@page import="java.awt.Color"%>
<%@page import="java.awt.Graphics2D"%>
<%@page import="java.awt.image.BufferedImage"%>
<%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%>

<%
	int width = 130;
	int height = 53;
	response.setContentType("image/jpeg");
	BufferedImage buffImg = new BufferedImage(width, height,
			BufferedImage.TYPE_INT_RGB);
	Graphics2D g = (Graphics2D) buffImg.getGraphics();
	Color c = new Color((int) (Math.random() * 35) + 219, (int) (Math
			.random() * 35) + 219, (int) (Math.random() * 35) + 219);
	g.setBackground(c);
	g.clearRect(0, 0, width, height);
	Color fc = new Color((int) (Math.random() * 71) + 55, (int) (Math
			.random() * 71) + 55, (int) (Math.random() * 71) + 55);
	g.setColor(fc);
	g.setFont(new Font("Times New Roman", Font.BOLD, 35));
	StringBuffer randomCode = new StringBuffer();
	int sp = (int) (Math.random() * 30) + 22;
	for (int i = 0; i < 4; i++) {
		char strRand = (char) (Math.random() > 0.50 ? (int) (Math.random() * 25) + 65
				: (int) (Math.random() * 25) + 97);
		g.drawString(String.valueOf(strRand), 24 * i + 12, sp
				+ (int) (Math.random() * 5));
		randomCode.append(strRand);
	}
	g.drawLine(0, sp - (int) (Math.random() * 20), 130, sp
			- (int) (Math.random() * 20));
	ServletOutputStream sos = response.getOutputStream();
	request.getSession().setAttribute("randomCode", randomCode.toString());
	JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);
	encoder.encode(buffImg);
	sos.close();
	//解决java.io.IOException: reading encoded JPEG Stream
	sos.flush();
	/**
	*下面两个out方法解决严重: Servlet.service() for servlet jsp threw exception
	java.lang.IllegalStateException: getOutputStream() has already been called for this response
	*/
	out.clear();
	out = pageContext.pushBody();
%>